<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"lishx.com","root":"/","scheme":"Mist","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"hide","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="软件安装安装dockerCentOS 7 使用yum作为安装程序。安装之前需要检查内核版本 12345678910111.检查内核版本uname -r 2.更新内核yum update3.安装Dockeryum install docker4.启动Dockersystemctl start docker">
<meta property="og:type" content="article">
<meta property="og:title" content="Redis基础">
<meta property="og:url" content="lishx.com/2020/09/20/utils/Redis/index.html">
<meta property="og:site_name" content="lishouxian&#39;s BLOG">
<meta property="og:description" content="软件安装安装dockerCentOS 7 使用yum作为安装程序。安装之前需要检查内核版本 12345678910111.检查内核版本uname -r 2.更新内核yum update3.安装Dockeryum install docker4.启动Dockersystemctl start docker">
<meta property="og:locale">
<meta property="article:published_time" content="2020-09-20T13:13:45.000Z">
<meta property="article:modified_time" content="2020-11-10T06:53:34.703Z">
<meta property="article:author" content="李寿贤">
<meta property="article:tag" content="学习">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="lishx.com/2020/09/20/utils/Redis/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-Hans'
  };
</script>

  <title>Redis基础 | lishouxian's BLOG</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">lishouxian's BLOG</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="lishx.com/2020/09/20/utils/Redis/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="李寿贤">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="lishouxian's BLOG">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Redis基础
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2020-09-20 21:13:45" itemprop="dateCreated datePublished" datetime="2020-09-20T21:13:45+08:00">2020-09-20</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2020-11-10 14:53:34" itemprop="dateModified" datetime="2020-11-10T14:53:34+08:00">2020-11-10</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/utils/" itemprop="url" rel="index"><span itemprop="name">utils</span></a>
                </span>
            </span>

          
            <span id="/2020/09/20/utils/Redis/" class="post-meta-item leancloud_visitors" data-flag-title="Redis基础" title="Views">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">Views: </span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine: </span>
    
    <a title="valine" href="/2020/09/20/utils/Redis/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/09/20/utils/Redis/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="软件安装"><a href="#软件安装" class="headerlink" title="软件安装"></a>软件安装</h2><h3 id="安装docker"><a href="#安装docker" class="headerlink" title="安装docker"></a>安装docker</h3><p>CentOS 7 使用yum作为安装程序。安装之前需要检查内核版本</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">1.检查内核版本</span><br><span class="line">uname -r </span><br><span class="line"></span><br><span class="line">2.更新内核</span><br><span class="line">yum update</span><br><span class="line"></span><br><span class="line">3.安装Docker</span><br><span class="line">yum install docker</span><br><span class="line"></span><br><span class="line">4.启动Docker</span><br><span class="line">systemctl start docker</span><br></pre></td></tr></table></figure>

<a id="more"></a>

<p>CentOS 8更改了安装程序，改而使用了dnf 作为安装程序。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">1. 安装Docker</span><br><span class="line">dnf install docker-ce --nobest</span><br><span class="line"></span><br><span class="line">2. 允许使用docker</span><br><span class="line">systemctl enable docker</span><br><span class="line"></span><br><span class="line">3. 启动Docker</span><br><span class="line">systemctl start docker</span><br></pre></td></tr></table></figure>

<h3 id="安装redis"><a href="#安装redis" class="headerlink" title="安装redis"></a>安装redis</h3><p>使用方法：镜像操作</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">1.搜索镜像</span><br><span class="line">docker search redis</span><br><span class="line"></span><br><span class="line">2.拉取镜像</span><br><span class="line">docker pull redis</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> Using default tag: latest 使用最新的标签</span></span><br><span class="line">3.查看当前镜像</span><br><span class="line">docker images</span><br><span class="line"><span class="meta">#</span><span class="bash">[root@aliC ~]<span class="comment"># docker images</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash">REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE</span></span><br><span class="line"><span class="meta">#</span><span class="bash">redis               latest              84c5f6e03bf0        6 hours ago         104MB</span></span><br><span class="line"><span class="meta">#</span><span class="bash">hello-world         latest              bf756fb1ae65        8 months ago        13.3kB</span></span><br><span class="line"></span><br><span class="line">4.下载指定版本镜像</span><br><span class="line">docker pull redis 版本</span><br><span class="line"></span><br><span class="line">5. 删除镜像</span><br><span class="line">docker rmi bf756fb1ae65 #镜像的id</span><br></pre></td></tr></table></figure>

<p>容器操作：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">1.根据镜像启动容器</span><br><span class="line">docker run -d -p 6379:6379 redis</span><br><span class="line"><span class="meta">#</span><span class="bash"> -p 是配置端口映射</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> -d 是后台运行</span></span><br><span class="line"></span><br><span class="line">3.查看运行中的容器</span><br><span class="line">docker ps</span><br><span class="line"></span><br><span class="line">4.停止容器</span><br><span class="line">docker stop 容器id</span><br><span class="line"></span><br><span class="line">5.查看所有的容器</span><br><span class="line">docker ps -a</span><br><span class="line"></span><br><span class="line">6.重新运行容器</span><br><span class="line">docker start 容器id</span><br><span class="line"></span><br><span class="line">7.删除容器</span><br><span class="line">docker rm 容器id</span><br></pre></td></tr></table></figure>

<p>阿里云的允许端口访问：要在控制台启用开放6379端口</p>
<h3 id="运行redis"><a href="#运行redis" class="headerlink" title="运行redis"></a>运行redis</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">1.进入redis目录</span><br><span class="line">docker exec -it myredis /bin/bash</span><br><span class="line"></span><br><span class="line">2.打开控制台</span><br><span class="line">redis-cli</span><br><span class="line"></span><br><span class="line">3.退出</span><br><span class="line">exit</span><br><span class="line"></span><br><span class="line">4.退出</span><br><span class="line">exit</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="redis的基本指令"><a href="#redis的基本指令" class="headerlink" title="redis的基本指令"></a>redis的基本指令</h2><h3 id="redis的五大基本数据类型"><a href="#redis的五大基本数据类型" class="headerlink" title="redis的五大基本数据类型"></a>redis的五大基本数据类型</h3><p>最常用的设置值和获取值</p>
 <figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">设置一对键值对</span></span><br><span class="line">127.0.0.1:6379&gt; set a a</span><br><span class="line">OK</span><br><span class="line"><span class="meta">#</span><span class="bash">获取键所对应的值</span></span><br><span class="line">127.0.0.1:6379&gt; get a</span><br><span class="line">&quot;a&quot;</span><br><span class="line"><span class="meta">#</span><span class="bash">是否存在某个键</span></span><br><span class="line">127.0.0.1:6379&gt; exists a</span><br><span class="line">(integer) 1</span><br><span class="line"><span class="meta">#</span><span class="bash">获取全部的键值对</span></span><br><span class="line">127.0.0.1:6379&gt; keys *</span><br><span class="line">1) &quot;a&quot;</span><br><span class="line">2) &quot;test&quot;</span><br><span class="line"><span class="meta">#</span><span class="bash">获取某键的类型</span></span><br><span class="line">127.0.0.1:6379&gt; type a</span><br><span class="line">string</span><br><span class="line"><span class="meta">#</span><span class="bash">删除某键</span></span><br><span class="line">127.0.0.1:6379&gt; del a</span><br><span class="line">(integer) 1</span><br><span class="line"><span class="meta">#</span><span class="bash">为键值对设置过期时间</span></span><br><span class="line">127.0.0.1:6379&gt; expire a 10</span><br><span class="line">(integer) 1</span><br><span class="line"><span class="meta">#</span><span class="bash">查看还有多少秒过期，-1代表永不过期，-2代表已经过期</span></span><br><span class="line">127.0.0.1:6379&gt; ttl a</span><br><span class="line">(integer) 8</span><br><span class="line">127.0.0.1:6379&gt; ttl a</span><br><span class="line">(integer) 2</span><br><span class="line"><span class="meta">#</span><span class="bash">清空当前库</span></span><br><span class="line">127.0.0.1:6379&gt; flushdb</span><br><span class="line">OK</span><br><span class="line"><span class="meta">#</span><span class="bash">清空所有库</span></span><br><span class="line">127.0.0.1:6379&gt; flushall</span><br><span class="line">OK</span><br><span class="line"><span class="meta">#</span><span class="bash">查看当前数据库的key的数量</span></span><br><span class="line">127.0.0.1:6379&gt; dbsize</span><br><span class="line">(integer) 1</span><br></pre></td></tr></table></figure>

<h3 id="String"><a href="#String" class="headerlink" title="String"></a>String</h3><p>String类型是redis中最基本的数据类型，一个redis中的字符串value最多可以是512M</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; set k1 123</span><br><span class="line">ok</span><br><span class="line"><span class="meta">#</span><span class="bash">追加</span></span><br><span class="line">127.0.0.1:6379&gt; append k1 ji</span><br><span class="line">(integer) 5</span><br><span class="line">127.0.0.1:6379&gt; get k1</span><br><span class="line">&quot;123ji&quot;</span><br><span class="line"><span class="meta">#</span><span class="bash">获取字符串长度</span></span><br><span class="line">127.0.0.1:6379&gt; strlen k1</span><br><span class="line">(integer) 5</span><br><span class="line"><span class="meta">#</span><span class="bash">只有在key不存在时设置key的值</span></span><br><span class="line">127.0.0.1:6379&gt; setnx k2 100</span><br><span class="line">(integer) 1</span><br><span class="line"><span class="meta">#</span><span class="bash">对数据值进行加减值的操作</span></span><br><span class="line">127.0.0.1:6379&gt; incr k2</span><br><span class="line">(integer) 101</span><br><span class="line">127.0.0.1:6379&gt; decr k2</span><br><span class="line">(integer) 100</span><br><span class="line">127.0.0.1:6379&gt; incrby k2 20</span><br><span class="line">(integer) 120</span><br><span class="line">127.0.0.1:6379&gt; decrby k2 50</span><br><span class="line">(integer) 70</span><br><span class="line"><span class="meta">#</span><span class="bash">对多个键值对进行操作</span></span><br><span class="line">mset mget msetnx </span><br><span class="line">127.0.0.1:6379&gt; set k1 qwertyui</span><br><span class="line">OK</span><br><span class="line"><span class="meta">#</span><span class="bash">获取闭区间的字符串的值的范围</span></span><br><span class="line">127.0.0.1:6379&gt; getrange k1 1 3</span><br><span class="line">&quot;wer&quot;</span><br><span class="line"><span class="meta">#</span><span class="bash">从起始位置开始覆盖值</span></span><br><span class="line">127.0.0.1:6379&gt; setrange k1 1 wqeqe</span><br><span class="line">(integer) 8</span><br><span class="line"><span class="meta">#</span><span class="bash">设置键值的同时设置过期时间</span></span><br><span class="line">setnx</span><br><span class="line"><span class="meta">#</span><span class="bash">设置键的同时获取旧的value的值</span></span><br><span class="line">getset</span><br></pre></td></tr></table></figure>

<h3 id="List"><a href="#List" class="headerlink" title="List"></a>List</h3><p>单键多值</p>
<p>是简单的字符串列表，他的底层是一个双向链表，对两端的操作性很高，通过下表操作中间的性能很差。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; rpop test</span><br><span class="line">&quot;5&quot;</span><br><span class="line">127.0.0.1:6379&gt; lpop test</span><br><span class="line">&quot;e&quot;</span><br><span class="line">127.0.0.1:6379&gt; rpush test2 x y z</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379&gt; rpoplpush test test2</span><br><span class="line">&quot;4&quot;</span><br><span class="line">127.0.0.1:6379&gt; lrange test 0 3</span><br><span class="line">1) &quot;d&quot;</span><br><span class="line">2) &quot;c&quot;</span><br><span class="line">3) &quot;b&quot;</span><br><span class="line">4) &quot;a&quot;</span><br><span class="line">127.0.0.1:6379&gt; lrange test 0 -1</span><br><span class="line">1) &quot;d&quot;</span><br><span class="line">2) &quot;c&quot;</span><br><span class="line">3) &quot;b&quot;</span><br><span class="line">4) &quot;a&quot;</span><br><span class="line">5) &quot;1&quot;</span><br><span class="line">6) &quot;2&quot;</span><br><span class="line">7) &quot;3&quot;</span><br><span class="line">127.0.0.1:6379&gt; lrem test 1 1</span><br></pre></td></tr></table></figure>

<h3 id="set"><a href="#set" class="headerlink" title="set"></a>set</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">给集合中添加指定元素</span></span><br><span class="line">127.0.0.1:6379&gt; sadd testSet a b c s d w qw 1 3 qw ftq</span><br><span class="line">(integer) 10</span><br><span class="line"><span class="meta">#</span><span class="bash">查看集合中的元素</span></span><br><span class="line">127.0.0.1:6379&gt; smembers testSet</span><br><span class="line"> 1) &quot;w&quot;</span><br><span class="line"> 2) &quot;d&quot;</span><br><span class="line"> 3) &quot;s&quot;</span><br><span class="line"> 4) &quot;1&quot;</span><br><span class="line"> 5) &quot;b&quot;</span><br><span class="line"> 6) &quot;qw&quot;</span><br><span class="line"> 7) &quot;a&quot;</span><br><span class="line"> 8) &quot;c&quot;</span><br><span class="line"> 9) &quot;ftq&quot;</span><br><span class="line">10) &quot;3&quot;</span><br><span class="line"><span class="meta">#</span><span class="bash">查看集合中是否存在某个元素</span></span><br><span class="line">127.0.0.1:6379&gt; sismember testSet a</span><br><span class="line">(integer) 1</span><br><span class="line"><span class="meta">#</span><span class="bash">删除集合中的某个元素</span></span><br><span class="line">127.0.0.1:6379&gt; srem testSet a 1</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; smembers testSet</span><br><span class="line">1) &quot;b&quot;</span><br><span class="line">2) &quot;qw&quot;</span><br><span class="line">3) &quot;w&quot;</span><br><span class="line">4) &quot;s&quot;</span><br><span class="line">5) &quot;c&quot;</span><br><span class="line">6) &quot;d&quot;</span><br><span class="line">7) &quot;ftq&quot;</span><br><span class="line">8) &quot;3&quot;</span><br><span class="line"><span class="meta">#</span><span class="bash">从集合中弹出一个元素(随机)</span></span><br><span class="line">127.0.0.1:6379&gt; spop testSet</span><br><span class="line">&quot;c&quot;</span><br><span class="line"><span class="meta">#</span><span class="bash">从集合中取出N个元素(并不删除)</span></span><br><span class="line">127.0.0.1:6379&gt; srandmember testSet 4</span><br><span class="line">1) &quot;qw&quot;</span><br><span class="line">2) &quot;w&quot;</span><br><span class="line">3) &quot;d&quot;</span><br><span class="line">4) &quot;b&quot;</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; sadd testSet1 a d g e s zx v s</span><br><span class="line">(integer) 7</span><br><span class="line"><span class="meta">#</span><span class="bash">取交集</span></span><br><span class="line">127.0.0.1:6379&gt; sinter testSet testSet1</span><br><span class="line">1) &quot;s&quot;</span><br><span class="line">2) &quot;d&quot;</span><br><span class="line"><span class="meta">#</span><span class="bash">取并集</span></span><br><span class="line">127.0.0.1:6379&gt; sunion testSet testSet1</span><br><span class="line"> 1) &quot;qw&quot;</span><br><span class="line"> 2) &quot;e&quot;</span><br><span class="line"> 3) &quot;g&quot;</span><br><span class="line"> 4) &quot;w&quot;</span><br><span class="line"> 5) &quot;s&quot;</span><br><span class="line"> 6) &quot;zx&quot;</span><br><span class="line"> 7) &quot;a&quot;</span><br><span class="line"> 8) &quot;d&quot;</span><br><span class="line"> 9) &quot;ftq&quot;</span><br><span class="line">10) &quot;3&quot;</span><br><span class="line">11) &quot;v&quot;</span><br><span class="line">12) &quot;b&quot;</span><br><span class="line"><span class="meta">#</span><span class="bash">取差集</span></span><br><span class="line">127.0.0.1:6379&gt; sdiff testSet testSet1</span><br><span class="line">1) &quot;3&quot;</span><br><span class="line">2) &quot;ftq&quot;</span><br><span class="line">3) &quot;qw&quot;</span><br><span class="line">4) &quot;w&quot;</span><br><span class="line">5) &quot;b&quot;</span><br></pre></td></tr></table></figure>

<h3 id="Hash"><a href="#Hash" class="headerlink" title="Hash"></a>Hash</h3><p>特别适合存储对象，是一个String类型的field和value的映射表</p>
<p>类似于Java里面的<code>Map&lt;String, String&gt;</code></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">设置一个Hash里的key-value</span></span><br><span class="line">127.0.0.1:6379&gt; hget set1 user</span><br><span class="line">&quot;1010&quot;</span><br><span class="line"><span class="meta">#</span><span class="bash">是否存在某个</span></span><br><span class="line">127.0.0.1:6379&gt; hexists set1 user</span><br><span class="line">(integer) 1</span><br><span class="line"><span class="meta">#</span><span class="bash">获取全部的value</span></span><br><span class="line">127.0.0.1:6379&gt; hvals set1</span><br><span class="line">1) &quot;1010&quot;</span><br><span class="line"><span class="meta">#</span><span class="bash">获取所有的key和value</span></span><br><span class="line">127.0.0.1:6379&gt; hgetall set1</span><br><span class="line">1) &quot;user&quot;</span><br><span class="line">2) &quot;1010&quot;</span><br><span class="line"><span class="meta">#</span><span class="bash">给数字的值添加一个数字</span></span><br><span class="line">127.0.0.1:6379&gt; hincrby set1 user 20</span><br><span class="line">(integer) 1030</span><br><span class="line">127.0.0.1:6379&gt; hsetnx set1 name 23</span><br><span class="line">(integer) 1</span><br></pre></td></tr></table></figure>

<h3 id="Zset"><a href="#Zset" class="headerlink" title="Zset"></a>Zset</h3><p>是一个没有重复元素的字符串集合。里面的每一个成员都关联了一个评分，这个评分被用来从最低分到最高分的方式排序集合中的成员。集合中的成员是唯一的但是评分是可以重复。</p>
<p>元素为键分数为值</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">添加成员的方式是`评分 成员`</span></span><br><span class="line">127.0.0.1:6379&gt; zadd zset1 100 a 20 b 300 c</span><br><span class="line">(integer) 3</span><br><span class="line"><span class="meta">#</span><span class="bash">查询所有的成员</span></span><br><span class="line">127.0.0.1:6379&gt; zrange zset1 0 -1</span><br><span class="line">1) &quot;b&quot;</span><br><span class="line">2) &quot;a&quot;</span><br><span class="line">3) &quot;c&quot;</span><br><span class="line">127.0.0.1:6379&gt; zadd zset1 400 a</span><br><span class="line">(integer) 0</span><br><span class="line">127.0.0.1:6379&gt; zrange zset1 0 -1</span><br><span class="line">1) &quot;b&quot;</span><br><span class="line">2) &quot;c&quot;</span><br><span class="line">3) &quot;a&quot;</span><br><span class="line"><span class="meta">#</span><span class="bash">查询指定分数</span></span><br><span class="line">127.0.0.1:6379&gt; zrangebyscore zset1 20 300</span><br><span class="line">1) &quot;b&quot;</span><br><span class="line">2) &quot;c&quot;</span><br><span class="line"><span class="meta">#</span><span class="bash">增加score，删除元素</span></span><br><span class="line">zincrby zrem zcount</span><br></pre></td></tr></table></figure>

<h2 id="redis缓存穿透和雪崩"><a href="#redis缓存穿透和雪崩" class="headerlink" title="redis缓存穿透和雪崩"></a>redis缓存穿透和雪崩</h2><h3 id="缓存穿透"><a href="#缓存穿透" class="headerlink" title="缓存穿透"></a>缓存穿透</h3><p>查一个不存在的数据的数据的时候，发现redis中没有命中就会去想持久层数据库查询。发现也没有就会请求失败。多次请求失败导致了缓存穿透问题</p>
<p>指的是对某个一定不存在的数据进行请求，该请求将会穿透缓存到达数据库。</p>
<p>解决方案：</p>
<ul>
<li>对这些不存在的数据缓存一个空数据；</li>
<li>对这类请求进行过滤。</li>
</ul>
<h4 id="布隆过滤器"><a href="#布隆过滤器" class="headerlink" title="布隆过滤器"></a>布隆过滤器</h4><p>布隆过滤器是一种数据结构，对所有可能查询的参数以hash的形式进行存储，在控制层先进行校验，不符合就丢弃，避免了对底层存储系统的查询压力。</p>
<h4 id="缓存空对象"><a href="#缓存空对象" class="headerlink" title="缓存空对象"></a>缓存空对象</h4><p>没有被命中时</p>
<p>问题：</p>
<ul>
<li>控制在缓存中的存在会浪费缓存的空间</li>
<li>即使设置了过期的时间也会有数据不一致的问题</li>
</ul>
<h3 id="缓存击穿"><a href="#缓存击穿" class="headerlink" title="缓存击穿"></a>缓存击穿</h3><p>有一个key非常的热点，查的量太大，在缓存过期的一瞬间大量的数据就会击穿缓存。 </p>
<p>解决方案：</p>
<h4 id="设置热点数据永不过期"><a href="#设置热点数据永不过期" class="headerlink" title="设置热点数据永不过期"></a>设置热点数据永不过期</h4><p>热点数据不设置过期时间也会造成缓存压力的问题</p>
<h4 id="加互斥锁"><a href="#加互斥锁" class="headerlink" title="加互斥锁"></a>加互斥锁</h4><p>每一个key同一时间只能有一个线程能去访问，其余进行等待</p>
<h3 id="缓存雪崩"><a href="#缓存雪崩" class="headerlink" title="缓存雪崩"></a>缓存雪崩</h3><p>在某个时间，缓存集中失效，Redis宕机。</p>
<p>解决方案：</p>
<h4 id="redis高可用"><a href="#redis高可用" class="headerlink" title="redis高可用"></a>redis高可用</h4><p>增加redis的数目，异地多活</p>
<h4 id="限流降级"><a href="#限流降级" class="headerlink" title="限流降级"></a>限流降级</h4><p>在缓存失效后可以使用加锁或者队列来控制读数据库写缓存的线程数量，例如对一个key同一时间只允许一个线程进行访问，其他线程都在等待。</p>
<h4 id="数据预热"><a href="#数据预热" class="headerlink" title="数据预热"></a>数据预热</h4><p>在正式部署之前，将可能的数据先预先访问一遍，是的大量的数据先去加载到缓存中。在即将发生大并发访问前手动触发加载缓存，让缓存失效的时间点尽量均匀。</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E5%AD%A6%E4%B9%A0/" rel="tag"># 学习</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/09/18/utils/Git/" rel="prev" title="Git基础学习">
      <i class="fa fa-chevron-left"></i> Git基础学习
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/09/20/utils/SQL-basic/" rel="next" title="sql基础1">
      sql基础1 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%BD%AF%E4%BB%B6%E5%AE%89%E8%A3%85"><span class="nav-number">1.</span> <span class="nav-text">软件安装</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%89%E8%A3%85docker"><span class="nav-number">1.1.</span> <span class="nav-text">安装docker</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%89%E8%A3%85redis"><span class="nav-number">1.2.</span> <span class="nav-text">安装redis</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BF%90%E8%A1%8Credis"><span class="nav-number">1.3.</span> <span class="nav-text">运行redis</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#redis%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%8C%87%E4%BB%A4"><span class="nav-number">2.</span> <span class="nav-text">redis的基本指令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#redis%E7%9A%84%E4%BA%94%E5%A4%A7%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="nav-number">2.1.</span> <span class="nav-text">redis的五大基本数据类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#String"><span class="nav-number">2.2.</span> <span class="nav-text">String</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#List"><span class="nav-number">2.3.</span> <span class="nav-text">List</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#set"><span class="nav-number">2.4.</span> <span class="nav-text">set</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Hash"><span class="nav-number">2.5.</span> <span class="nav-text">Hash</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Zset"><span class="nav-number">2.6.</span> <span class="nav-text">Zset</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#redis%E7%BC%93%E5%AD%98%E7%A9%BF%E9%80%8F%E5%92%8C%E9%9B%AA%E5%B4%A9"><span class="nav-number">3.</span> <span class="nav-text">redis缓存穿透和雪崩</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BC%93%E5%AD%98%E7%A9%BF%E9%80%8F"><span class="nav-number">3.1.</span> <span class="nav-text">缓存穿透</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%B8%83%E9%9A%86%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="nav-number">3.1.1.</span> <span class="nav-text">布隆过滤器</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%BC%93%E5%AD%98%E7%A9%BA%E5%AF%B9%E8%B1%A1"><span class="nav-number">3.1.2.</span> <span class="nav-text">缓存空对象</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BC%93%E5%AD%98%E5%87%BB%E7%A9%BF"><span class="nav-number">3.2.</span> <span class="nav-text">缓存击穿</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%AE%BE%E7%BD%AE%E7%83%AD%E7%82%B9%E6%95%B0%E6%8D%AE%E6%B0%B8%E4%B8%8D%E8%BF%87%E6%9C%9F"><span class="nav-number">3.2.1.</span> <span class="nav-text">设置热点数据永不过期</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%8A%A0%E4%BA%92%E6%96%A5%E9%94%81"><span class="nav-number">3.2.2.</span> <span class="nav-text">加互斥锁</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BC%93%E5%AD%98%E9%9B%AA%E5%B4%A9"><span class="nav-number">3.3.</span> <span class="nav-text">缓存雪崩</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#redis%E9%AB%98%E5%8F%AF%E7%94%A8"><span class="nav-number">3.3.1.</span> <span class="nav-text">redis高可用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%99%90%E6%B5%81%E9%99%8D%E7%BA%A7"><span class="nav-number">3.3.2.</span> <span class="nav-text">限流降级</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%95%B0%E6%8D%AE%E9%A2%84%E7%83%AD"><span class="nav-number">3.3.3.</span> <span class="nav-text">数据预热</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">李寿贤</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">40</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">13</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">李寿贤</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://mist.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a>
  </div>

        






<script>
  (function() {
    function leancloudSelector(url) {
      url = encodeURI(url);
      return document.getElementById(url).querySelector('.leancloud-visitors-count');
    }

    function addCount(Counter) {
      var visitors = document.querySelector('.leancloud_visitors');
      var url = decodeURI(visitors.id);
      var title = visitors.dataset.flagTitle;

      Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({ url })))
        .then(response => response.json())
        .then(({ results }) => {
          if (results.length > 0) {
            var counter = results[0];
            leancloudSelector(url).innerText = counter.time + 1;
            Counter('put', '/classes/Counter/' + counter.objectId, { time: { '__op': 'Increment', 'amount': 1 } })
              .catch(error => {
                console.error('Failed to save visitor count', error);
              });
          } else {
              Counter('post', '/classes/Counter', { title, url, time: 1 })
                .then(response => response.json())
                .then(() => {
                  leancloudSelector(url).innerText = 1;
                })
                .catch(error => {
                  console.error('Failed to create', error);
                });
          }
        })
        .catch(error => {
          console.error('LeanCloud Counter Error', error);
        });
    }

    function showTime(Counter) {
      var visitors = document.querySelectorAll('.leancloud_visitors');
      var entries = [...visitors].map(element => {
        return decodeURI(element.id);
      });

      Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({ url: { '$in': entries } })))
        .then(response => response.json())
        .then(({ results }) => {
          for (let url of entries) {
            let target = results.find(item => item.url === url);
            leancloudSelector(url).innerText = target ? target.time : 0;
          }
        })
        .catch(error => {
          console.error('LeanCloud Counter Error', error);
        });
    }

    let { app_id, app_key, server_url } = {"enable":true,"app_id":"xb8ocBOeUvnh1ClRKD6b7Vwm-gzGzoHsz","app_key":"ODH211TGDI1jVYfBVxifNiXt","server_url":null,"security":false};
    function fetchData(api_server) {
      var Counter = (method, url, data) => {
        return fetch(`${api_server}/1.1${url}`, {
          method,
          headers: {
            'X-LC-Id'     : app_id,
            'X-LC-Key'    : app_key,
            'Content-Type': 'application/json',
          },
          body: JSON.stringify(data)
        });
      };
      if (CONFIG.page.isPost) {
        if (CONFIG.hostname !== location.hostname) return;
        addCount(Counter);
      } else if (document.querySelectorAll('.post-title-link').length >= 1) {
        showTime(Counter);
      }
    }

    let api_server = app_id.slice(-9) !== '-MdYXbMMI' ? server_url : `https://${app_id.slice(0, 8).toLowerCase()}.api.lncldglobal.com`;

    if (api_server) {
      fetchData(api_server);
    } else {
      fetch('https://app-router.leancloud.cn/2/route?appId=' + app_id)
        .then(response => response.json())
        .then(({ api_server }) => {
          fetchData('https://' + api_server);
        });
    }
  })();
</script>


      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>















  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : '9AvQGweVbH47hbtztejqnkta-gzGzoHsz',
      appKey     : 'lo0cfhvPh5MqPkgzHzT5n0Az',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
